package leet.ku._190;

public class Solution {

	// you need treat n as an unsigned value
    public int reverseBits(int n) {
    	n = ((n & 0x0000FFFF) << 16) | (((n & 0xFFFF0000) >> 16) & 0x0000FFFF);
    	n = ((n & 0x00FF00FF) << 8) | (((n & 0xFF00FF00) >> 8) & 0x00FF00FF);
    	n = ((n & 0x0F0F0F0F) << 4) | (((n & 0xF0F0F0F0) >> 4) & 0x0F0F0F0F);
    	n = ((n & 0x33333333) << 2) | (((n & 0xCCCCCCCC) >> 2) & 0x33333333);
    	n = ((n & 0x55555555) << 1) | (((n & 0xAAAAAAAA) >> 1) & 0x55555555);
    	return n;
    }
    
    public static void main(String[] args) {
    	Solution s = new Solution();
    	System.out.println(s.reverseBits(43261596));
    	System.out.println(s.reverseBits(Integer.MIN_VALUE));
    }

}
